package org.example.basic.day05;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo05集合练习 {
    public static void main(String[] args) {
        SetMenue();

    }
    public static void SetMenue(){
        List<MenuInfo> list1 = new ArrayList<>();
        list1.add(new MenuInfo(1,"系统管理","0",null,null));
        list1.add(new MenuInfo(2,"用户管理","1","1",null));
        list1.add(new MenuInfo(3,"密码修改","1","1",null));
        list1.add(new MenuInfo(4,"角色菜单管理","1","1",null));
        list1.add(new MenuInfo(5,"菜单管理","1","1",null));
        list1.add(new MenuInfo(8,"课程管理","0",null,null));
        list1.add(new MenuInfo(9,"试题管理","1","8",null));
        list1.add(new MenuInfo(10,"科目管理","1","8",null));
        list1.add(new MenuInfo(11,"试卷管理","1","8",null));
        list1.add(new MenuInfo(12,"班级管理","0",null,null));
        list1.add(new MenuInfo(13,"班级查询","1","12",null));
        list1.add(new MenuInfo(14,"学生信息管理","1","12",null));
        list1.add(new MenuInfo(15,"成绩管理","1","12",null));
        list1.add(new MenuInfo(16,"成绩分段","1","12",null));
        list1.add(new MenuInfo(17,"考试记录","1","8",null));
        for (MenuInfo o:list1){
            System.out.println( o);
        }
        System.out.println("=============================清洗后================================");
        setMenu03(list1);
    }

    public static void setMenu03(List<MenuInfo> list1){
        Map<Integer,MenuInfo> map = new HashMap<>();
        for(MenuInfo o:list1){
            map.put(o.getMenuId(),o);
        }

        for (MenuInfo o:list1){
            if(o.getParentId()!=null){
                    int i = Integer.parseInt(o.getParentId());
                    MenuInfo menuInfo=map.get(i);
                    List<MenuInfo> subs = new ArrayList<>();
                    if(menuInfo.getSubs()==null){
                        menuInfo.setSubs(subs);
                    }
                    if(menuInfo!=null){
                       menuInfo.getSubs().add(o);
                    }
                }
            }
        // 输出一级菜单及其子菜单
        for (MenuInfo menu : list1) {
            if ("0".equals(menu.getMenuDegree())) {
                System.out.println("menuInfo = " + menu.toString().split(", subs=")[0] + ",\nsubs=");
                for (MenuInfo sub : menu.getSubs()) {
                    System.out.println("  " + sub);
                }
                System.out.println("]");
            }
        }
        }
    public static void SetMenue2( List<MenuInfo> list1){
        for (MenuInfo o:list1){
            if(o.getMenuDegree().equals("0")){
                List<MenuInfo> subs = new ArrayList<>();
                for (MenuInfo o2:list1){
                    if(o2.getParentId()!=null) {
                        int i = Integer.parseInt(o2.getParentId());
                        if (i == o.getMenuId()) {
                            subs.add(o2);
                        }
                    }
                }
                o.setSubs(subs);
                System.out.println("menuInfo = " + o.toString().split(", subs=")[0] + ",\nsubs=");
                for (MenuInfo sub : o.getSubs()) {
                    System.out.println("  " + sub);
                }
                System.out.println("]");
            }
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class MenuInfo{
    /*** 菜单id */
    private int menuId;
    /*** 菜单标题 */
    private String title;
    /*** 菜单等级 */
    private String menuDegree;
    /*** 父级菜单id */
    private String parentId;
    /*** 子菜单 */
    private List<MenuInfo> subs;

}
